package com.casa.POI.dianping;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Property;
import org.htmlcleaner.TagNode;
import org.htmlcleaner.XPatherException;



public class FetchListUrlsExecutor extends CityThreadsExecutor {
	
	public FetchListUrlsExecutor(int cityId) {
		super(cityId);
	}

	public List<ListUrl> fetchListUrls(String pageUrlStr) throws IOException, XPatherException {
		List<ListUrl> listUrls = new ArrayList<ListUrl>();
		TagNode node = SpiderUtil.cleanUrl(pageUrlStr);
		Object[] results = node.evaluateXPath("//a[@class='B f14']");
		for (Object result : results) {
			String url = ((TagNode) result).getAttributeByName("href");
			ListUrl lu = new ListUrl(url, pageUrlStr, getCityId(), 0);
			listUrls.add(lu);
		}
		return listUrls;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	protected List<? extends Runnable> getNextBatchWorks() {
		Session s = SessionManager.getInstance().openSession();
		Criteria c = s.createCriteria(ListPageUrl.class);
		c.add(Property.forName("cityId").eq(getCityId()));
		c.add(Property.forName("totalList").isNull());
		c.setMaxResults(ConfigurationManager.getInstance().getBatchSize());
		List<ListPageUrl> pageUrls = c.list();
		s.close();
		List<Workitem> works = new ArrayList<Workitem>();
		for (ListPageUrl pageUrl : pageUrls) {
			works.add(new Workitem(pageUrl));
		}
		return works;
	}
	
	private synchronized void saveListUrls(List<ListUrl> listUrls, ListPageUrl pageUrl) {
		Session s = SessionManager.getInstance().openSession();
		pageUrl.setTotalList(listUrls.size());
		s.beginTransaction();
		s.update(pageUrl);
		for (ListUrl listUrl : listUrls) {
			if (s.get(ListUrl.class, listUrl.getUrl()) == null) {
				s.save(listUrl);
				printStatus(listUrl.getUrl());
			}
		}
		s.getTransaction().commit();
		s.close();
	}
	
	class Workitem implements Runnable {
		
		private ListPageUrl pageUrl;

		public Workitem(ListPageUrl pageUrl) {
			this.pageUrl = pageUrl;
		}

		@Override
		public void run() {
			String pageUrlStr = pageUrl.getUrl();
			List<ListUrl> listUrls = null;
			
			try {
				listUrls = fetchListUrls(pageUrlStr);
			} catch (Exception e) {
				e.printStackTrace();
				return;
			}
			
			saveListUrls(listUrls, pageUrl);
		}
	}
	
	public static void main(String[] args) {
		new FetchListUrlsExecutor(149).execute();
	}

}
